package com.ruoyi.web;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.MyLoginService;
import com.ruoyi.framework.web.service.MyTokenService;
import com.ruoyi.system.domain.Company;
import com.ruoyi.system.domain.Hr;
import com.ruoyi.system.service.ICompanyService;
import com.ruoyi.system.service.IHrService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.service.IMyloginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
//前端hr注册的控制器
public class HrviewController extends BaseController {
    @Autowired
    private IHrService hrService;
    @Autowired
    private MyLoginService myLoginService;
    @Autowired
    private MyTokenService myTokenService;
    @Autowired
    private ICompanyService companyService;
    @RequestMapping("/hrsignup")
    public AjaxResult hrsignup(@RequestParam("signupname")String name, @RequestParam("phone")String phone, @RequestParam("pwd")String pwd, @RequestParam("hrpos")String hrpos, @RequestParam("hrcompany") Long hrcompany){
        Hr temhr = hrService.selectHrByHrphone(phone);
        if(temhr!=null){//存在注册失败
            return new AjaxResult(1,"已经存在该HR账号,请直接登录");
        }
        Hr hr=new Hr();
      hr.setHrTel(phone);
      hr.setHrPwd(pwd);
      hr.setHrPos(hrpos);
      hr.setCompanyId(hrcompany);
      hr.setHrName(name);
      return toAjax(hrService.insertHr(hr));
    }

    @RequestMapping("/hrdetail")
    public AjaxResult hrdetail(HttpServletRequest request, HttpServletResponse response){
        String token = request.getParameter("token");
        Long hrid = myTokenService.getloginid(token);
        Hr hr = hrService.selectHrByHrId(hrid);

        Company company = companyService.selectCompanyByCompanyId(hr.getCompanyId());
        Map<String,Object> data = new HashMap<>();
        data.put("hr",hr);
        data.put("company",company);
        return new AjaxResult(200,"返回成功",data);

    }

    @RequestMapping("/updatehrdetail")
    public AjaxResult updatehrdetail(@RequestParam String hrid, @RequestParam String hrname, @RequestParam String hrTel, @RequestParam String hrpwd,@RequestParam String hrpos){
        Hr hr = hrService.selectHrByHrId(Long.valueOf(hrid));
        hr.setHrName(hrname);
        hr.setHrPos(hrpos);
        hr.setHrPwd(hrpwd);
       return toAjax(hrService.updateHr(hr));
    }

}
